package com.itheima.controller;

import com.alibaba.druid.sql.visitor.functions.Lcase;
import com.itheima.pojo.DataParam;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.time.LocalDate;
import java.util.List;
@Slf4j
@RestController
public class EmpController {
    //修改员工
    @Autowired
    private EmpService empService;
    @PutMapping("/emps")

    public Result updateEmp(@RequestBody Emp emp){
        empService.update(emp);
        return Result.success();
    }
    //删除员工
    @DeleteMapping("/emps/{ids}")
    public Result deleteEmps(@PathVariable List<Integer> ids){
        empService.deleteEmps(ids);

        return Result.success();
    }
    //员工列表查询


        //条件分页查询
        //@CrossOrigin
      //  @GetMapping("/emps")

        public Result page(@RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "10") Integer pageSize,
                           String name, Short gender,
                           @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                           @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
            //记录日志
            log.info("分页查询，参数：{},{},{},{},{},{}", page, pageSize,name, gender, begin, end);
            //调用业务层分页查询功能
            PageBean pageBean = empService.page(page, pageSize, name, gender, begin, end);
            //响应
            return Result.success(pageBean);
        }

        @GetMapping("/emps")
            public Result page(@RequestParam(defaultValue = "1")Integer page,
                               @RequestParam(defaultValue = "10") Integer pageSize,
                               String name, Integer gender,
                               @DateTimeFormat(pattern="yyyy-MM-dd") LocalDate begin,
                               @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate end
                               ){
        log.info("请求获取参数姓名：{}，gender:{},begin:{},end:{},page:{},pageSize:{}",name,gender,begin,end,page,pageSize);
        //调取业务层代码
            PageBean pageBean = empService.pageDemo(page, pageSize, name, gender, begin, end);

            return Result.success(pageBean);
        }
   /* @GetMapping("/emps")//分页查询
    public Result queryEmps(String name, Integer gender, LocalDate begin, LocalDate end,Integer page,Integer pageSize){
        Integer total = empService.queryEmpsStepOne(name,gender,begin,end);
        DataParam dataParam = new DataParam();
        List<Emp> emps = empService.queryEmpsStepTwo(name, gender, begin, end, page, pageSize);
        Object[] array = emps.toArray();
        dataParam.setTotal(total);
        dataParam.setRows(array);
        if (emps!=null&&emps.size()>0){


            return Result.success(dataParam);
        }
        return Result.error("查询错误");
    }*/
    //根据id查询
    @GetMapping("/emps/{id}")
    public Result getEmpsbyId(@PathVariable Integer id){
        Emp emp = empService.getEmpById(id);
        return Result.success(emp);
    }
    //添加员工
    @PostMapping("/emps")
    public Result addEmp(@RequestBody Emp emp){
        empService.addEmp(emp);
        return Result.success();
    }

    //文件上传
  /*  @PostMapping("/upload")
    public Result upload(File file){

        return Result.success();
    }*/


}
